<?php
class Process_data extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}

	function get_paper($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('tbl_paper');
			return $query->result_array();
		}
		$query = $this->db->get_where('tbl_paper', array('id' => $slug));
		return $query->result_array();
	}


	function get_paperByUserId($userId = FALSE)
	{
		if ($userId === FALSE)
		{
			return FALSE;
		}
		$query = $this->db->get_where('tbl_paper', array('posted_by' => $userId));
		return $query->result_array();
	}

	function get_main_subjects($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('det_main_subjects');
			return $query->result_array();
		}
		$query = $this->db->get_where('det_main_subjects', array('id' => $slug));
		return $query->result_array();
	}

	function get_subjects($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('det_subjects');
			return $query->result_array();
		}
		$query = $this->db->get_where('det_subjects', array('id' => $slug));
		return $query->result_array();
	}

	function get_subjectsMainsubject($slug = FALSE)
	{
		$query = $this->db->get_where('det_subjects', array('main_subjects_ref' => $slug));
		return $query->result_array();
	}

	function get_approval_status($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('det_approval_status');
			return $query->result_array();
		}
		$query = $this->db->get_where('det_approval_status', array('id' => $slug));
		return $query->result_array();
	}

	function get_job_role($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('det_job_role');
			return $query->result_array();
		}
		$query = $this->db->get_where('det_job_role', array('id' => $slug));
		return $query->result_array();
	}

	function get_organization_type($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('det_organization_type');
			return $query->result_array();
		}
		$query = $this->db->get_where('det_organization_type', array('id' => $slug));
		return $query->result_array();
	}

	function get_area_of_interest($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('det_area_of_interest');
			return $query->result_array();
		}
		$query = $this->db->get_where('det_area_of_interest', array('id' => $slug));
		return $query->result_array();
	}


	function get_det_country($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('det_country');
			return $query->result_array();
		}
		$query = $this->db->get_where('det_country', array('id' => $slug));
		return $query->result_array();
	}

	function isEmailExisting($emailId){
		$query = $this->db->get_where('tbl_users', array('email' => $emailId));
		$rs =  $query->result_array();
		if(count($rs)>0)
		return true;
		else
		return false;
	}

	function userDetails($emailId){
		//Password encryption process


		$this->db->select('id, username, email, role, verification_code, verification_status, registered_on');
		$query = $this->db->get_where('tbl_users', array('email' => $emailId));
		return $query->result_array();
	}

	function usersFullDetails($emailId){
		$query = $this->db->query('SELECT * FROM tbl_author WHERE user_ref = (select id from tbl_users where email = \''.$emailId.'\')');
		return $query->result_array();
	}

	function usersFullDetails_Id($id){
		$query = $this->db->query('SELECT * FROM tbl_author WHERE user_ref = (select id from tbl_users where id = \''.$id.'\')');
		return $query->result_array();
	}


	function matchAndGetUserDetail($emailId, $password){
		//Password encryption process


		$this->db->select('id, username, email, role, verification_code, verification_status, registered_on');
		$query = $this->db->get_where('tbl_users', array('email' => $emailId, 'password' => $password));
		return $query->result_array();
	}

	function matchAndGetAdminDetail($emailId, $password){
		//Password encryption process


		$this->db->select('id, name, email');
		$query = $this->db->get_where('tbl_admin_users', array('email' => $emailId, 'password' => $password));
		return $query->result_array();
	}

	function user_registration_step(){
		//email, password, cpassword

		if(!isset($_POST['userid']))	{

			$data = array(
				'email' => $this->input->post('email'),
				'password' => $this->input->post('password')
			);

			$this->db->insert('tbl_users', $data);
			$userId = $this->db->insert_id();
			$data1 = array(
				'user_ref' => $userId,

				'first_name' => $this->input->post('first_name'),
				'phone_number' => $this->input->post('phone_number'),
				'address' => $this->input->post('mailing_address'),
				'city' => $this->input->post('city'),
				'state' => $this->input->post('state'),
				'postal_code' => $this->input->post('zip'),
				'country_ref' => $this->input->post('country'),
				'job_title' => $this->input->post('job_title'),

				'org_name' => $this->input->post('org_name'),
				'org_type_ref' => $this->input->post('organization_type'),
				'job_role_ref' => $this->input->post('job_role'),
				'area_of_interest_ref' => implode(($this->input->post('area_of_interest')==null)?array():$this->input->post('area_of_interest'), ',')
			);
			$this->db->insert('tbl_author', $data1);
			return $userId;
			//$this->input->post('email_subscriptions');
		}else	{
			$data1 = array(
				'user_ref' => $_POST['userid'],

				'first_name' => $this->input->post('first_name'),
				'phone_number' => $this->input->post('phone_number'),
				'address' => $this->input->post('mailing_address'),
				'city' => $this->input->post('city'),
				'state' => $this->input->post('state'),
				'postal_code' => $this->input->post('zip'),
				'country_ref' => $this->input->post('country'),
				'job_title' => $this->input->post('job_title'),

				'org_name' => $this->input->post('org_name'),
				'org_type_ref' => $this->input->post('organization_type'),
				'job_role_ref' => $this->input->post('job_role'),
				'area_of_interest_ref' => implode(($this->input->post('area_of_interest')==null)?array():$this->input->post('area_of_interest'), ',')
			);
			$this->db->where('user_ref', $_POST['userid']);
			$this->db->update('tbl_author', $data1);
			return $_POST['userid'];
		}
	}


	function changePassword()	{
		$data = array(
				'password' => $this->input->post('password')
		);
		$this->db->where('id', $_POST['userid']);
		$this->db->update('tbl_users', $data);
	}

	function get_adminEmails()
	{
		$query = $this->db->query('SELECT email FROM tbl_admin_users');
		return $query->result_array();
	}


	function getEditorialMembers($slug = FALSE)
	{
		if ($slug === FALSE)
		{
			$query = $this->db->get('tbl_editorial_members');
			return $query->result_array();
		}
		$query = $this->db->get_where('tbl_editorial_members', array('id' => $slug));
		return $query->result_array();
	}


	function addEditorialMembers($slug = FALSE)
	{
		$data = array(
			'email' => $this->input->post('email'),
			'name' => $this->input->post('name'),
			'phone' => $this->input->post('phone'),
			'designation' => $this->input->post('designation'),
		);

		if(trim($this->input->post('email')) != '')
		{
			if($this->input->get('id') == 0)	{
				$this->db->insert('tbl_editorial_members', $data);
			}else {
				$this->db->where('id', $this->input->get('id'));
				$this->db->update('tbl_editorial_members', $data);
			}
		}

	}

	function addMainSubjects($slug = FALSE)
	{
		$data = array(
			'name' => $this->input->post('mainsubject'),
		);

		if(trim($this->input->post('mainsubject')) != '')
		{
			if($this->input->get('id') == 0)	{
				$this->db->insert('det_main_subjects', $data);
			}else {
				$this->db->where('id', $this->input->get('id'));
				$this->db->update('det_main_subjects', $data);
			}
		}

	}

	function addSubjects($slug = FALSE)
	{
		$data = array(
			'name' => $this->input->post('mainsubject'),
			'main_subjects_ref' => $this->input->post('main_subjects_ref')
		);

		if(trim($this->input->post('mainsubject')) != '')
		{
			if($this->input->get('id') == 0)	{
				$this->db->insert('det_subjects', $data);
			}else {
				$this->db->where('id', $this->input->get('id'));
				$this->db->update('det_subjects', $data);
			}
		}

	}


	function enquirey()	{
		date_default_timezone_set('Asia/Kolkata');
		if(trim($this->input->post('email')) != '')
		{
			$data = array(
				'name' => $this->input->post('name'),
				'email' => $this->input->post('email'),
				'phone' => $this->input->post('phone'),
				'requirement' => $this->input->post('requirement'),
				'date' => date( "Y-m-d h:i:s a", now())
			);
			
			$this->db->insert('tbl_enquirey', $data);
		}
	}

}

?>
